What is io-ts-types?
The io-ts-types package provides additional types and combinators for use with io-ts, a runtime type system for IO decoding/encoding in TypeScript. It extends the functionality of io-ts by offering more complex and useful types that are commonly needed in real-world applications.
What are io-ts-types's main functionalities?
DateFromISOString
This feature allows you to decode ISO string dates into JavaScript Date objects. It is useful for handling date strings in JSON APIs.
const { DateFromISOString } = require('io-ts-types');
const t = require('io-ts');
const DateType = DateFromISOString;
const result = DateType.decode('2023-10-01T00:00:00Z');
console.log(result); // Right(new Date('2023-10-01T00:00:00Z'))
NonEmptyString
This feature ensures that a string is not empty. It is useful for validating user input where empty strings are not allowed.
const { NonEmptyString } = require('io-ts-types');
const t = require('io-ts');
const NonEmptyStringType = NonEmptyString;
const result = NonEmptyStringType.decode('Hello');
console.log(result); // Right('Hello')
UUID
This feature validates that a string is a valid UUID. It is useful for ensuring that identifiers conform to the UUID format.
const { UUID } = require('io-ts-types');
const t = require('io-ts');
const UUIDType = UUID;
const result = UUIDType.decode('123e4567-e89b-12d3-a456-426614174000');
console.log(result); // Right('123e4567-e89b-12d3-a456-426614174000')
Option
This feature allows you to handle optional values in a functional way using the Option type from fp-ts. It is useful for dealing with nullable fields in a type-safe manner.
const { optionFromNullable } = require('io-ts-types');
const t = require('io-ts');
const OptionType = optionFromNullable(t.string);
const result = OptionType.decode(null);
console.log(result); // Right(none)
const result2 = OptionType.decode('Hello');
console.log(result2); // Right(some('Hello'))
Other packages similar to io-ts-types
zod
Zod is a TypeScript-first schema declaration and validation library. It provides a similar functionality to io-ts-types but with a different API and philosophy. Zod is more focused on developer experience and ease of use, while io-ts-types is more focused on functional programming paradigms.
yup
Yup is a JavaScript schema builder for value parsing and validation. It is similar to io-ts-types in that it provides a way to define and validate complex data structures. However, Yup is more widely used in the React ecosystem and has a more imperative API compared to the functional approach of io-ts-types.
superstruct
Superstruct is a library for validating and transforming data structures in JavaScript and TypeScript. It offers similar functionality to io-ts-types but with a simpler and more intuitive API. Superstruct is designed to be lightweight and easy to use, making it a good alternative for those who prefer a less functional approach.
A collection of runtime types and combinators for use with io-ts
Installation
To install the stable version:
npm i io-ts-types
Note. io-ts-types
depends on
starting from 0.5.0
you must install fp-ts
, io-ts
, newtype-ts
and monocle-ts
manually (fp-ts
, io-ts
, newtype-ts
and monocle-ts
are listed in peerDependency
)
TypeScript compatibility
io-ts-types version | required typescript version |
---|
0.5.x | 3.5+ |
0.4.x | 3.1+ |
Documentation